package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.Professional;
import com.ruoyi.personnel.service.IProfessionalService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 职称信息Controller
 *
 * @author ruoyi
 * @date 2021-02-25
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/personnel/professional" )
public class ProfessionalController extends BaseController {

    private final IProfessionalService iProfessionalService;

    /**
     * 查询职称信息列表
     */
    @PreAuthorize("@ss.hasPermi('personnel:professional:list')")
    @GetMapping("/list")
    public TableDataInfo list(Professional professional) {
        startPage();
        List<Professional> list = iProfessionalService.queryList(professional);
        return getDataTable(list);
    }

    /**
     * 导出职称信息列表
     */
    @PreAuthorize("@ss.hasPermi('personnel:professional:export')" )
    @Log(title = "职称信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Professional professional) {
        List<Professional> list = iProfessionalService.queryList(professional);
        ExcelUtil<Professional> util = new ExcelUtil<Professional>(Professional.class);
        return util.exportExcel(list, "professional" );
    }

    /**
     * 获取职称信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:professional:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iProfessionalService.getById(id));
    }

    /**
     * 新增职称信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:professional:add')" )
    @Log(title = "职称信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Professional professional) {
        professional.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iProfessionalService.save(professional) ? 1 : 0);
    }

    /**
     * 修改职称信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:professional:edit')" )
    @Log(title = "职称信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Professional professional) {
        return toAjax(iProfessionalService.updateById(professional) ? 1 : 0);
    }

    /**
     * 删除职称信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:professional:remove')" )
    @Log(title = "职称信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iProfessionalService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
